package EventBus.demo;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;

import EventBus.demo.eventEntity.BaseEvent;
import EventBus.demo.eventEntity.UserEvent;
import EventBus.demo.lister.BaseLister;
import EventBus.demo.lister.UserLister;
import utils.ThreadUtils;

/**
 * Created by wuzhao on 2017/4/7 0007.
 */
public class EventUtils {

    private static EventBus eventBus;

    private static AsyncEventBus asyncEventBus;

    static {
        eventBus = new EventBus();
        asyncEventBus = new AsyncEventBus(ThreadUtils.getPoolTaskExecutor());
    }

    public static <T extends BaseLister> boolean reg(T sub){
        if (sub == null)
            return false;
        eventBus.register(sub);
        return true;
    }

    public static <T extends BaseLister> boolean asyncReg(T sub){
        if (sub == null)
            return false;
        asyncEventBus.register(sub);
        return true;
    }

    public static <E extends BaseEvent> boolean asyncPost(E entity){
        if (entity == null)
            return false;
        asyncEventBus.post(entity);
        return true;
    }

    public static <E extends BaseEvent> boolean post(E entity){
        if (entity == null)
            return false;
        eventBus.post(entity);
        return true;
    }

    public static void main(String[] args) {
        EventUtils.reg(new UserLister());
        EventUtils.post(new UserEvent("名字","id"));
    }

}
